<template>
  <div class="login-wrapper">
    <div class="modal">
      <div class="title">后台管理系统</div>
      <el-form :model="user" status-icon :rules="rules" ref="loginForm">
        <el-form-item prop="userName">
          <el-input
            type="text"
            prefix-icon="User"
            v-model="user.userName"
          ></el-input>
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input
            type="password"
            prefix-icon="View"
            v-model="user.userPwd"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      user: {
        userName: "",
        userPwd: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        userPwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  // mounted() {
  //  this.$request.get('/login', {name: 'lisi'}, {mock: true, loading: true}).then(res => {
  //    console.log(res)
  //  })
  // },
  methods: {
    login() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          try {
            this.$api.login(this.user).then((data) => {
              // 登录成功
              // console.log("接收返回结果:", data)
              this.$store.commit("saveUserInfo", data);
              this.$message.success("登录成功,欢迎回来," + data.userName);
              // const that = this
              // setTimeout(()=>{
              //   that.$router.push('/welcome')
              // }, 3000)
              // 跳转到欢迎页
              this.$router.push("/welcome");
            });
          } catch (error) {
            console.error(error);
          }
        } else {
          console.error("error");
        }
      });
    },
  },
};
</script>

<style lang="scss">
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f9fcff;
  width: 100vw;
  height: 100vh;
  .modal {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 0 1px 3px #c9c4dd;
    .title {
      font-size: 30px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }
    .btn-login {
      width: 100%;
    }
  }
}
</style>
